package com.example.shopping_mall.utils;

import android.util.Log;

import com.example.shopping_mall.application.BaseApplication;
import com.example.shopping_mall.bean.BannerBean;
import com.example.shopping_mall.bean.BannerMiddleBean;
import com.example.shopping_mall.bean.BannerObjectBean;
import com.example.shopping_mall.bean.BrandManager;
import com.example.shopping_mall.bean.BrandManagerLast;
import com.example.shopping_mall.bean.BrandManagerMiddle;
import com.example.shopping_mall.bean.BrandMiddleBean;
import com.example.shopping_mall.bean.GoodFreshBean;
import com.example.shopping_mall.bean.KillDetailBean;
import com.example.shopping_mall.bean.KillDetailMidPerBean;
import com.example.shopping_mall.bean.KillDetailMiddleBean;
import com.example.shopping_mall.bean.KillDetailPerBean;
import com.example.shopping_mall.bean.RecommendBean;
import com.example.shopping_mall.bean.RecommendListBean;
import com.example.shopping_mall.bean.SecondBean;
import com.example.shopping_mall.bean.SecondLastBean;
import com.example.shopping_mall.bean.SecondMiddleBean;
import com.example.shopping_mall.bean.SkuHeadBean;
import com.example.shopping_mall.bean.SkuLastBean;
import com.example.shopping_mall.bean.SkuMiddleBean;
import com.example.shopping_mall.bean.UiSecondBean;
import com.example.shopping_mall.fragment.ShoppingMall_home_Fragment;
import com.example.shopping_mall.http.ShoppingRequest;

import org.json.JSONArray;
import org.json.JSONObject;

import retrofit2.http.Field;
import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

//首页推荐的persenter
public class ShoppingUtils implements ShoppingRequest {

    private static final String TAG = ShoppingMall_home_Fragment.class.getSimpleName();

    //轮播图
    @Override
    public void requestAdvertise(int status,OnHomeAdvertiseRequestListener listener) {
        BaseApplication.shoppingInterface.requestHomeAdvertise(status)
                .compose(new RxjavaIntegration.SchedulerTransformer<BannerObjectBean<BannerMiddleBean>>())
                .subscribe(new Subscriber<BannerObjectBean<BannerMiddleBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BannerObjectBean<BannerMiddleBean> bannerMiddleBeanBannerObjectBean) {
                        BannerBean[] beans=bannerMiddleBeanBannerObjectBean.getData().getList();
                        listener.OnHomeAdvertiseRequest(beans);
                    }
                });
    }

    //品牌制造商
    @Override
    public void requestBrandApp(int recommendStatus, OnHomeBrandAppRequestListener listener) {
        BaseApplication.shoppingInterface.requestHomeBrandApp(recommendStatus)
                .compose(new RxjavaIntegration.SchedulerTransformer<RecommendListBean<BrandMiddleBean>>())
                .subscribe(new Subscriber<RecommendListBean<BrandMiddleBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(RecommendListBean<BrandMiddleBean> brandMiddleBeanRecommendListBean) {
                        BrandMiddleBean[] beans=brandMiddleBeanRecommendListBean.getData();
                        //Log.d(TAG, "BrandMiddleBean啊ppppppp: "+beans);
                        listener.OnHomeBrandAppRequest(beans);
                    }
                });
    }

    //品牌详情
    @Override
    public void requestHomeBrandAppMore(int brandId, OnfindSmsHomeBrandAppListener listener) {
        BaseApplication.shoppingInterface.requesfindSmsHomeBrandApp(brandId)
                .compose(new RxjavaIntegration.SchedulerTransformer<RecommendListBean<BrandMiddleBean>>())
                .subscribe(new Subscriber<RecommendListBean<BrandMiddleBean>>() {
                    @Override
                    public void onCompleted() {
                        Log.d(TAG, "33333333333333: ");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "2222222222222: ");
                    }

                    @Override
                    public void onNext(RecommendListBean<BrandMiddleBean> brandMiddleBeanRecommendListBean) {
                        BrandMiddleBean[] middleBeans=brandMiddleBeanRecommendListBean.getData();
                        Log.d(TAG, "init111111111111111111111: "+middleBeans);
                        listener.OnfindSmsHomeBrandAppRequest(middleBeans);
                    }
                });
    }

    //品牌更多推荐
    @Override
    public void requestPmsBrand(OnPmsBrandRequestListener listener) {
        BaseApplication.shoppingInterface.requestfindPmsBrand()
                .compose(new RxjavaIntegration.SchedulerTransformer<BannerObjectBean<BrandManagerMiddle<BrandManagerLast>>>())
                .subscribe(new Subscriber<BannerObjectBean<BrandManagerMiddle<BrandManagerLast>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BannerObjectBean<BrandManagerMiddle<BrandManagerLast>> brandManagerMiddleBannerObjectBean) {
                        BrandManager[] managers=brandManagerMiddleBannerObjectBean.getData().getPmsBrandList().getList();
                        listener.OnPmsBrandRequest(managers);
                    }
                });
    }

    //品牌更多推荐详情
    @Override
    public void requestBrand(int id, int factoryStatus, OnfindPmsBrandRequestListener listener) {
        BaseApplication.shoppingInterface.requestPmsBrand(id,factoryStatus)
                .compose(new RxjavaIntegration.SchedulerTransformer<BannerObjectBean<BrandManagerMiddle<BrandManagerLast>>>())
                .subscribe(new Subscriber<BannerObjectBean<BrandManagerMiddle<BrandManagerLast>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BannerObjectBean<BrandManagerMiddle<BrandManagerLast>> brandManagerMiddleBannerObjectBean) {
                        BrandManager[] brandManagers=brandManagerMiddleBannerObjectBean.getData().getPmsBrandList().getList();
                        listener.OnfindPmsBrandRequest(brandManagers);
                    }
                });
    }

    //秒杀专区
    @Override
    public void requestProductRelation(OnPromotionProductRelationRequestListener listener) {
        BaseApplication.shoppingInterface.requestPromotionProductRelation()
                .compose(new RxjavaIntegration.SchedulerTransformer<BannerObjectBean<SecondMiddleBean>>())
                .subscribe(new Subscriber<BannerObjectBean<SecondMiddleBean>>() {
                    @Override
                    public void onCompleted() {
                        //Log.d(TAG, "444444444: ");
                    }

                    @Override
                    public void onError(Throwable e) {
                        //Log.d(TAG, "3333333: ");
                    }

                    @Override
                    public void onNext(BannerObjectBean<SecondMiddleBean> secondMiddleBeanBannerObjectBean) {
                        SecondLastBean[] lastBeans=secondMiddleBeanBannerObjectBean.getData().getList();
                        //Log.d(TAG, "BrandMiddleBean啊: "+lastBeans);
                        listener.OnPromotionProductRelationAppRequest(lastBeans);
                    }
                });
    }

    //秒杀专区场次表
    @Override
    public void requestPromotionSession(OnFlashPromotionSessionRequestListener listener) {
        BaseApplication.shoppingInterface.requestFlashPromotionSession()
                .compose(new RxjavaIntegration.SchedulerTransformer<BannerObjectBean<KillDetailMiddleBean>>())
                .subscribe(new Subscriber<BannerObjectBean<KillDetailMiddleBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BannerObjectBean<KillDetailMiddleBean> killDetailMiddleBeanBannerObjectBean) {
                        KillDetailBean[] killDetailBeans=killDetailMiddleBeanBannerObjectBean.getData().getList();
                        listener.OnFlashPromotionSessionRequest(killDetailBeans);
                    }
                });
    }

    //秒杀详情列表
    @Override
    public void requestRelation(int flashPromotionId,int start,int page, OnProductRelationRequestListener listener) {
        BaseApplication.shoppingInterface.requestFlashPromotionProductRelation(flashPromotionId,start,page)
                .compose(new RxjavaIntegration.SchedulerTransformer<BannerObjectBean<SecondMiddleBean>>())
                .subscribe(new Subscriber<BannerObjectBean<SecondMiddleBean>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(BannerObjectBean<SecondMiddleBean> secondMiddleBeanBannerObjectBean) {
                        SecondLastBean[] list=secondMiddleBeanBannerObjectBean.getData().getList();
                        //Log.d(TAG, "BrandMiddleBean啊: "+lastBeans);
                        listener.OnProductRelationRequest(list);
                    }
                });
    }

    //秒杀专区限购管理（时间）
    @Override
    public void requestSmsFlashPromotion(OnSmsFlashPromotionRequestListener listener) {
        BaseApplication.shoppingInterface.requestfindSmsFlashPromotion()
                .compose(new RxjavaIntegration.SchedulerTransformer<BannerObjectBean<KillDetailMidPerBean>>())
                .subscribe(new Subscriber<BannerObjectBean<KillDetailMidPerBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BannerObjectBean<KillDetailMidPerBean> killDetailMidPerBeanBannerObjectBean) {
                        KillDetailPerBean[] beans=killDetailMidPerBeanBannerObjectBean.getData().getList();
                        listener.OnSmsFlashPromotionRequest(beans);
                    }
                });
    }


    //新鲜好物
    @Override
    public void requestNewProductPic(int recommendStatus, OnNewProductPicRequestListener listener) {
        BaseApplication.shoppingInterface.requestHomeNewProductPic(recommendStatus)
                .compose(new RxjavaIntegration.SchedulerTransformer<RecommendListBean<GoodFreshBean>>())
                .subscribe(new Subscriber<RecommendListBean<GoodFreshBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(RecommendListBean<GoodFreshBean> goodFreshBeanRecommendListBean) {
                        GoodFreshBean[] beans=goodFreshBeanRecommendListBean.getData();
                        listener.OnNewProductRequest(beans);
                    }
                });
    }

    //人气推荐
    @Override
    public void requestRecommendProductPic(OnRecommendProductPicRequestListener listener) {
        BaseApplication.shoppingInterface.requestHomeRecommendProductPic()
                .compose(new RxjavaIntegration.SchedulerTransformer<RecommendListBean<RecommendBean>>())
                .subscribe(new Subscriber<RecommendListBean<RecommendBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(RecommendListBean<RecommendBean> recommendBeanRecommendListBean) {
                        RecommendBean[] recommendBeans=recommendBeanRecommendListBean.getData();
                        listener.OnRecommendProductPicRequest(recommendBeans);
//                        Log.d(TAG, "recommendBeans: "+recommendBeans);
                    }
                });

    }

    @Override
    public void requestProductAttribute(OnPmsProductAttributeRequestListener listener) {
        BaseApplication.shoppingInterface.requestPmsProductAttribute()
                .compose(new RxjavaIntegration.SchedulerTransformer<BannerObjectBean<SkuHeadBean<SkuMiddleBean>>>())
                .subscribe(new Subscriber<BannerObjectBean<SkuHeadBean<SkuMiddleBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BannerObjectBean<SkuHeadBean<SkuMiddleBean>> skuHeadBeanBannerObjectBean) {
                        SkuLastBean[] beans=skuHeadBeanBannerObjectBean.getData().getList().getList();
                        listener.OnProductAttributeRequest(beans);
                    }
                });
    }


    //轮播图
    public interface OnHomeAdvertiseRequestListener{
        void OnHomeAdvertiseRequest(BannerBean[] bannerBeans);
    }

    //品牌制造商
    public interface OnHomeBrandAppRequestListener{
        void OnHomeBrandAppRequest(BrandMiddleBean[] middleBeans);
    }

    //品牌详情
    public interface OnfindSmsHomeBrandAppListener{
        void OnfindSmsHomeBrandAppRequest(BrandMiddleBean[] beans);
    }

    //品牌更多推荐
    public interface OnPmsBrandRequestListener{
        void OnPmsBrandRequest(BrandManager[] brandManagers);
    }

    //品牌更多推荐详情
    public interface OnfindPmsBrandRequestListener{
        void OnfindPmsBrandRequest(BrandManager[] managers);
    }

    //秒杀专区首页列表
    public interface OnPromotionProductRelationRequestListener{
        void OnPromotionProductRelationAppRequest(SecondLastBean[] lastBeans);
    }

    //秒杀专区场次表
    public interface OnFlashPromotionSessionRequestListener{
        void OnFlashPromotionSessionRequest(KillDetailBean[] detailBeans);
    }

    //秒杀详情列表
    public interface OnProductRelationRequestListener{
        void OnProductRelationRequest(SecondLastBean[] beans);
    }

    //秒杀专区限购管理（时间）
    public interface OnSmsFlashPromotionRequestListener{
        void OnSmsFlashPromotionRequest(KillDetailPerBean[] killDetailPerBeans);
    }

    //新鲜好物
    public interface OnNewProductPicRequestListener{
        void OnNewProductRequest(GoodFreshBean[] goodFreshBeans);
    }

    //人气推荐
    public interface OnRecommendProductPicRequestListener{
        void OnRecommendProductPicRequest(RecommendBean[] beans);
    }

    public interface OnPmsProductAttributeRequestListener{
        void OnProductAttributeRequest(SkuLastBean[] lastBeans);
    }
}
